package arrival;

import dbhandler.Booking;
import dbhandler.Room;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class RoomDraw {

    private MouseAdapter mouseAdapterPressed;
    private JScrollPane pane;
    private String inDate;
    private String outDate;
    private String selectedRoom;
    private JPanel masterPanel;
    public final static Color GREEN = new Color(40, 217, 98);;

    /**
     * The constructor used initializing the necessary components and variables.
     * @param pane
     * @param inDate
     * @param outDate
     */
    public RoomDraw(JScrollPane pane, String inDate, String outDate) {
        this.pane = pane;
        this.inDate = inDate;
        this.outDate = outDate;
        actionListener(); 
    }

    /**
     * Method that is used to draw individual panels for every available room in
     * the given period between the dates. Also draws information about the room
     * on the associated panel.
     */
    public void draw() {
        ArrayList<String> rooms = new ArrayList<String>();
        rooms = renderStringOfRooms();
        masterPanel = new JPanel();
        JPanel headerPanel = new JPanel();
        int mPanelHeight = rooms.size() * 22 + 22;

        masterPanel.setPreferredSize(new Dimension(pane.getWidth() - 35, mPanelHeight));
        masterPanel.setLayout(new FlowLayout(0, 0, 1));
        masterPanel.setVisible(true);
        masterPanel.setBackground(Color.WHITE);

        headerPanel.setPreferredSize(new Dimension(pane.getWidth() - 35, 20));
        headerPanel.setBackground(Color.WHITE);
        headerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        headerPanel.setLayout(new GridLayout(1, 7));

        JLabel hRoomLabel = new JLabel("Room #");
        JLabel hSingleLabel = new JLabel("Single Room");
        JLabel hDoubleLabel = new JLabel("Double Room");
        JLabel hSuiteLabel = new JLabel("Suite");
        JLabel hOceanViewLabel = new JLabel("Ocean View");
        JLabel hAccessibleLabel = new JLabel("Accessible");
        JLabel hExecutiveLabel = new JLabel("Executive");
        headerPanel.add(hRoomLabel);
        headerPanel.add(hSingleLabel);
        headerPanel.add(hDoubleLabel);
        headerPanel.add(hSuiteLabel);
        headerPanel.add(hOceanViewLabel);
        headerPanel.add(hAccessibleLabel);
        headerPanel.add(hExecutiveLabel);

        masterPanel.add(headerPanel);
        pane.getViewport().add(masterPanel);

        for (int i = 0; i < rooms.size(); i++) {
            JPanel panel = new JPanel();
            JLabel roomLabel = new JLabel();
            JLabel singleLabel = new JLabel();
            JLabel doubleLabel = new JLabel();
            JLabel suiteLabel = new JLabel();
            JLabel oceanViewLabel = new JLabel();
            JLabel accessibleLabel = new JLabel();
            JLabel executiveLabel = new JLabel();

            Room r = new Room(rooms.get(i));

            roomLabel.setText(rooms.get(i));
            roomLabel.setVisible(true);

            int tempCat = r.getCategory().getCategoryNo();
            boolean tempOcv = r.isOceanView();
            boolean tempAccs = r.isHandicap();
            boolean tempExe = r.isExecutive();

            if (tempCat == 1) {
                singleLabel.setText("Yes");
                doubleLabel.setText("No");
                suiteLabel.setText("No");
                singleLabel.setForeground(Color.BLACK);
                doubleLabel.setForeground(Color.RED);
                suiteLabel.setForeground(Color.RED);
            }
            if (tempCat == 2) {
                singleLabel.setText("No");
                doubleLabel.setText("Yes");
                suiteLabel.setText("No");
                singleLabel.setForeground(Color.RED);
                doubleLabel.setForeground(Color.BLACK);
                suiteLabel.setForeground(Color.RED);
            }
            if (tempCat == 3) {
                singleLabel.setText("No");
                doubleLabel.setText("No");
                suiteLabel.setText("Yes");
                singleLabel.setForeground(Color.RED);
                doubleLabel.setForeground(Color.RED);
                suiteLabel.setForeground(Color.BLACK);
            }
            if (tempOcv == true) {
                oceanViewLabel.setText("Yes");
                oceanViewLabel.setForeground(Color.BLACK);
            } else {
                oceanViewLabel.setText("No");
                oceanViewLabel.setForeground(Color.RED);
            }
            if (tempAccs == true) {
                accessibleLabel.setText("Yes");
                accessibleLabel.setForeground(Color.BLACK);
            } else {
                accessibleLabel.setText("No");
                accessibleLabel.setForeground(Color.RED);
            }
            if (tempExe == true) {
                executiveLabel.setText("Yes");
                executiveLabel.setForeground(Color.BLACK);
            } else {
                executiveLabel.setText("No");
                executiveLabel.setForeground(Color.RED);
            }

            singleLabel.setVisible(true);
            doubleLabel.setVisible(true);
            suiteLabel.setVisible(true);
            oceanViewLabel.setVisible(true);
            accessibleLabel.setVisible(true);
            executiveLabel.setVisible(true);

            panel.setPreferredSize(new Dimension(pane.getWidth() - 35, 20));
            panel.setVisible(true);
            panel.setLayout(new GridLayout(1, 7));

            panel.add(roomLabel);
            panel.add(singleLabel);
            panel.add(doubleLabel);
            panel.add(suiteLabel);
            panel.add(oceanViewLabel);
            panel.add(accessibleLabel);
            panel.add(executiveLabel);

            panel.setBackground(Color.WHITE);
            panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            panel.addMouseListener(mouseAdapterPressed);

            masterPanel.add(panel);
        }
    }

    /**
     * Support method used to render an ArrayList of Room objects, for every 
     * room that is listed in the Database.
     *
     * @return An ArrayList of every Room-object. 
     */
    private ArrayList<Room> renderRoomList() {
        ArrayList<Room> roomList = new ArrayList<Room>();
        ArrayList<Date> dateList = new ArrayList<Date>();

        roomList = new Room().getAllRooms();

        for (int i = 0; i < roomList.size(); i++) {
            dateList = roomList.get(i).getDateArray();
            roomList.get(i).setDateList(dateList);
        }
        return roomList;
    }

    /**
     * Support method used to create a list of the rooms that are available in
     * the selected period.
     *
     * @return An ArrayList of all the available rooms in the given time-frame,
     * as Strings.
     */
    private ArrayList<String> renderStringOfRooms() {
        ArrayList<String> strList = new ArrayList<String>();
        ArrayList<Date> bookingDates = new Booking().getIntermediateDays(inDate, outDate);
        ArrayList<Room> roomList = renderRoomList();
        ArrayList<String> tempStrList = new ArrayList<String>();

        for (int i = 0; i < roomList.size(); i++) {
            ArrayList<Date> tempDate = roomList.get(i).getDateList();
            for (int j = 0; j < tempDate.size(); j++) {
                for (int k = 0; k < bookingDates.size(); k++) {
                    if (tempDate.get(j).equals(bookingDates.get(k))) {
                        tempStrList.add(roomList.get(i).getRoomNo());
                    }
                }
            }
        }
        for (int i = 0; i < roomList.size(); i++) {
            for (int j = 0; j < tempStrList.size(); j++) {
                if (roomList.get(i).getRoomNo().equalsIgnoreCase(tempStrList.get(j))) {
                    roomList.remove(i);
                }
            }
        }
        for (int i = 0; i < roomList.size(); i++) {
            strList.add(roomList.get(i).getRoomNo());
        }

        return strList;
    }

    /**
     * Accessor method to get the Room number that the user choose.
     * @return The selected room number.
     */
    public String getSelectedRoom() {
        return selectedRoom;
    }

    /**
     * Method to initialize the actionlisteners. Only one actionlistener
     * is currently implemented, which is the mousePressed listener.
     * The mousePressed listener gets the roomnumber from label on the panel
     * that is clicked on, and afterwards sets the selected panels color to green.
     */
    private void actionListener() {
        mouseAdapterPressed = (new java.awt.event.MouseAdapter() {

            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                panelMousePressed(evt);
            }

            private void panelMousePressed(MouseEvent evt) {

                for (int i = 0; i < masterPanel.getComponentCount(); i++) {
                    JPanel tempPan = (JPanel) masterPanel.getComponent(i);
                    tempPan.setBackground(Color.WHITE);
                }

                JPanel panel = (JPanel) evt.getSource();
                JLabel label = (JLabel) panel.getComponent(0);
                panel.setBackground(GREEN );

                selectedRoom = label.getText();
            }
        });
    }
}
